package com.luna.cloudworm.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.luna.cloudworm.product.dao.AttrGroupDao;
import com.luna.cloudworm.product.entity.AttrEntity;
import com.luna.cloudworm.product.entity.AttrGroupEntity;
import com.luna.cloudworm.product.service.AttrGroupService;
import com.luna.cloudworm.product.service.AttrService;
import com.luna.cloudworm.product.service.SpuInfoService;
import com.luna.cloudworm.product.vo.AttrGroupWithAttrsVo;
import com.luna.cloudworm.product.vo.SpuItemAttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    AttrService attrService;
    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 带条件的分页查询
     *
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        IPage<AttrGroupEntity> page = null;
        String key = (String) params.get("key");
        // select * from pms_attr_group  where catelog_id = x and (attr_group_id = xx or attr_group_name like %xx%)
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> obj.eq("attr_group_id", key).or().like("attr_group_name", key));
        }
        if (catelogId == 0) {
            // 查询全部
            page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
        } else {
            queryWrapper.and((obj) ->
                    obj.eq("catelog_id", catelogId));
            page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );

        }
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 根据分类，获取分类下面的所有分组
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        // 根据分组id 查询关系表获取所有的属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream()
                .map(group -> {
                    AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
                    BeanUtils.copyProperties(group, attrsVo);
                    List<AttrEntity> relationAttr = attrService.getRelationAttr(group.getAttrGroupId());
                    attrsVo.setAttrs(relationAttr);
                    return attrsVo;
                }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 通过spu 的到分类id 然后根据分类id查询pms_attr_group 属性分组信息（id，name），
     * 然后根据 groupid 查询pms_attr_attrgroup_relation拿到所有属性的信息
     *
     * @param spuId
     * @param catalogId
     * @return
     */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 查询当前spu 对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        return vos;
    }

}